// $codepro.audit.disable lossOfPrecisionInCast, variableShouldBeFinal, accessorMethodNamingConvention
// *******************************************
// Loss of precision in cast is acceptable in this case
// because it does not affect the correctness of the game
// *******************************************
// Variables cannot be final because they are modified in the constructor
// *******************************************
// All the methods with names begin by "set" do not mean to be setters
/**
* Import libraries that are needed for the game
*/
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import java.awt.Font;

/**
* View class which plays a role as a GUI
* 
* @author Hung Lac
* CS 2340 - Fall 2012
*  * @version $Revision: 1.0 $

*/
@SuppressWarnings("serial")
public class View extends JFrame {

	/**
	 * Field model.
	 */
	private Model model = null;

	/**
	 * Field parent.
	 */
	private View parent;

	/**
	 * Field lblTechLevel2.
	 */
	/**
	 * Field lblResource2.
	 */
	/**
	 * Field lblPlanetName2.
	 */
	/**
	 * Field lblBaysFilled.
	 */
	/**
	 * Field lblTechLevel.
	 */
	/**
	 * Field lblResource.
	 */
	/**
	 * Field lblPlanetName.
	 */
	/**
	 * Field lblNewLabel.
	 */
	/**
	 * Field lblFuel.
	 */
	/**
	 * Field lblCurrency.
	 */
	/**
	 * Field lblHealth.
	 */
	public JLabel lblHealth, lblCurrency, lblFuel, lblNewLabel,
	lblPlanetName, lblResource, lblTechLevel, lblBaysFilled, 
	lblPlanetName2, lblResource2, lblTechLevel2;

	/**
	 * Field menuBar.
	 */
	private final JMenuBar menuBar ;

	/**
	 * Field mnGame.
	 */
	private final JMenu mnGame;

	/**
	 * Field mntmSave.
	 */
	/**
	 * Field mntmExit.
	 */
	/**
	 * Field mntmLoad.
	 */
	/**
	 * Field mntmNew.
	 */
	public JMenuItem mntmNew, mntmLoad, mntmExit, mntmSave;

	/**
	 * Field panel.
	 */
	/**
	 * Field navigation.
	 */
	/**
	 * Field market.
	 */
	public JPanel market, navigation, panel;

	/**
	 * Field btnShipYard.
	 */
	/**
	 * Field btnHireMercenary.
	 */
	/**
	 * Field btnBuysellEquipment.
	 */
	/**
	 * Field btnRepairfuel.
	 */
	/**
	 * Field btnTravel.
	 */
	/**
	 * Field btnNews.
	 */
	public JButton btnNews, btnTravel, btnRepairfuel, 
	btnBuysellEquipment, btnHireMercenary, btnShipYard;

	/**
	 * Field table_5.
	 */
	/**
	 * Field table_4.
	 */
	/**
	 * Field table_3.
	 */
	/**
	 * Field table_2.
	 */
	/**
	 * Field table_1.
	 */
	/**
	 * Field table.
	 */
	public JTable table, table_1, table_2, table_3, table_4, table_5;

	/**
	 * Field sell.
	 */
	/**
	 * Field buy.
	 */
	public JButton[] buy, sell;

	/**
	 * Create the frame as the main screen for the game
	 * @param model
	 */
	public View(Model model) {
		setTitle("Space Trader");
		this.model=model;//input model
		parent=this;//set parent as this frame
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(new Dimension(800, 600));//set default size
		setResizable( false );
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnGame = new JMenu("Game");
		menuBar.add(mnGame);
		
		mntmNew = new JMenuItem("New");
		mnGame.add(mntmNew);
		
		mntmLoad = new JMenuItem("Load");
		mnGame.add(mntmLoad);
		
		mntmSave = new JMenuItem("Save");
		mnGame.add(mntmSave);
		
		mntmExit = new JMenuItem("Exit");
		mnGame.add(mntmExit);
		getContentPane().setLayout(null);
		
		market = new JPanel();
		market.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		market.setBounds(244, 8, 530, 298);
		getContentPane().add(market);
		market.setLayout(null);
		
		buy= new JButton[10];
		for(int i=0; i < buy.length; i++){
			buy[i]= new JButton(" ");
			buy[i].setBounds(340, 59 + i * 16, 50, 15);
			market.add(buy[i]);
		}
		
		sell= new JButton[10];
		for(int i=0; i < sell.length; i++){
			sell[i]= new JButton(" ");
			sell[i].setBounds(210, 59 + i * 16, 50, 15);
			market.add(sell[i]);
		}
		
		table_5 = new JTable();
		table_5.setShowGrid(false);
		table_5.setFillsViewportHeight(true);
		table_5.setBounds(147, 59, 46, 214);
		table_5.setModel(new DefaultTableModel(
				new Object[][] {
					{null},
					{null},
					{null},
					{null},
					{null},
					{null},
					{null},
					{null},
					{null},
					{null},
				},
				new String[] {
					"New column"
				}
			));
		market.add(table_5);
		
		table_4 = new JTable();
		table_4.setShowGrid(false);
		table_4.setModel(new DefaultTableModel(
			new Object[][] {
				{null},
				{null},
				{null},
				{null},
				{null},
				{null},
				{null},
				{null},
				{null},
				{null},
			},
			new String[] {
				"New column"
			}
		));
		table_4.setBounds(220, 59, 40, 214);
		market.add(table_4);
		
		table_3 = new JTable();
		table_3.setShowGrid(false);
		table_3.setBackground(Color.WHITE);
		table_3.setModel(new DefaultTableModel(
				new Object[][] {
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
					},
			new String[] {
				"New column"
			}
		));
		table_3.setFillsViewportHeight(true);
		table_3.setBounds(348, 59, 40, 214);
		market.add(table_3);
		
		table_2 = new JTable();
		table_2.setModel(new DefaultTableModel(
			new Object[][] {
				{null},
				{null},
				{null},
				{null},
				{null},
				{null},
				{null},
				{null},
				{null},
				{null},
			},
			new String[] {
				"New column"
			}
		));
		table_2.setShowGrid(false);
		table_2.setFillsViewportHeight(true);
		table_2.setBounds(275, 59, 46, 214);
		market.add(table_2);
		
		table_1 = new JTable();
		table_1.setEnabled(false);
		table_1.setShowGrid(false);
		table_1.setFillsViewportHeight(true);
		table_1.setModel(new DefaultTableModel(
				new Object[][] {
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
						{null},
					},
			new String[] {
				"New column"
			}
		));
		table_1.setBounds(10, 59, 127, 214);
		market.add(table_1);
		
		final JLabel lblMarketPlace = new JLabel("Market Place");
		lblMarketPlace.setBounds(10, 11, 136, 14);
		market.add(lblMarketPlace);
		
		final JLabel lblSell = new JLabel("Sell");
		lblSell.setBounds(191, 36, 46, 14);
		market.add(lblSell);
		
		final JLabel lblBuy = new JLabel("Buy");
		lblBuy.setBounds(317, 36, 46, 14);
		market.add(lblBuy);
		
		final JLabel lblTargetPlanet_1 = new JLabel("Target Planet");
		lblTargetPlanet_1.setBounds(422, 36, 83, 14);
		market.add(lblTargetPlanet_1);
		
		table = new JTable();
		table.setEnabled(false);
		table.setShowHorizontalLines(false);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
			},
			new String[] {
				"New column", "New column", "New column", "New column"
			}
		));
		table.setBounds(10, 33, 510, 240);
		market.add(table);
		
		lblBaysFilled = new JLabel("Bays Filled");
		lblBaysFilled.setBounds(10, 275, 127, 23);
		market.add(lblBaysFilled);
		
		lblCurrency = new JLabel("Currency");
		lblCurrency.setBounds(246, 276, 142, 20);
		market.add(lblCurrency);
		
		navigation = new JPanel();
		navigation.setBorder(new SoftBevelBorder
				(BevelBorder.RAISED, null, null, null, null));
		navigation.setLocation(244, 317);
		navigation.setSize(306, 215);
		getContentPane().add(navigation);
		navigation.setLayout(null);
		
		final JLabel lblNavigation = new JLabel("Navigation");
		lblNavigation.setBounds(4, 4, 104, 14);
		navigation.add(lblNavigation);
		
		final JPanel currentPlanet = new JPanel();
		currentPlanet.setBorder(new SoftBevelBorder
				(BevelBorder.RAISED, null, null, null, null));
		currentPlanet.setBounds(10, 317, 224, 215);
		getContentPane().add(currentPlanet);
		currentPlanet.setLayout(null);
		
		final JLabel lblCurrentPlanet = new JLabel("Current Planet");
		lblCurrentPlanet.setBounds(4, 4, 115, 14);
		currentPlanet.add(lblCurrentPlanet);
		
		btnNews = new JButton("News");
		btnNews.setBounds(66, 181, 89, 23);
		currentPlanet.add(btnNews);
		
		lblPlanetName = new JLabel("Name:");
		lblPlanetName.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblPlanetName.setBounds(14, 29, 187, 16);
		currentPlanet.add(lblPlanetName);
		
		lblResource = new JLabel("Resource: ");
		lblResource.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblResource.setBounds(14, 70, 187, 16);
		currentPlanet.add(lblResource);
		
		lblTechLevel = new JLabel("Tech Level:");
		lblTechLevel.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblTechLevel.setBounds(14, 107, 187, 16);
		currentPlanet.add(lblTechLevel);
		
		final JPanel targetPlanet = new JPanel();
		targetPlanet.setBorder(new SoftBevelBorder
				(BevelBorder.RAISED, null, null, null, null));
		targetPlanet.setBounds(560, 317, 214, 215);
		getContentPane().add(targetPlanet);
		targetPlanet.setLayout(null);
		
		lblPlanetName2 = new JLabel("Name:");
		lblPlanetName2.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblPlanetName2.setBounds(14, 29, 187, 16);
		targetPlanet.add(lblPlanetName2);
		
		lblResource2 = new JLabel("Resource: ");
		lblResource2.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblResource2.setBounds(14, 70, 187, 16);
		targetPlanet.add(lblResource2);
		
		lblTechLevel2 = new JLabel("Tech Level:");
		lblTechLevel2.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblTechLevel2.setBounds(14, 107, 187, 16);
		targetPlanet.add(lblTechLevel2);
		
		final JLabel lblTargetPlanet = new JLabel("Target Planet");
		lblTargetPlanet.setBounds(4, 4, 120, 14);
		targetPlanet.add(lblTargetPlanet);
		
		btnTravel = new JButton("Travel");
		btnTravel.setBounds(64, 181, 89, 23);
		targetPlanet.add(btnTravel);
		
		final JPanel spaceStation = new JPanel();
		spaceStation.setBorder(new SoftBevelBorder
				(BevelBorder.RAISED, null, null, null, null));
		spaceStation.setBounds(10, 11, 224, 164);
		getContentPane().add(spaceStation);
		spaceStation.setLayout(null);
		
		final JLabel lblSpaceStation = new JLabel("Space Station");
		lblSpaceStation.setBounds(4, 4, 129, 14);
		spaceStation.add(lblSpaceStation);
		
	    btnBuysellEquipment = new JButton("Buy/Sell Equipment");
		btnBuysellEquipment.setBounds(35, 29, 147, 23);
		spaceStation.add(btnBuysellEquipment);
		
	    btnRepairfuel = new JButton("Repair/Fuel");
		btnRepairfuel.setBounds(35, 63, 147, 23);
		spaceStation.add(btnRepairfuel);
		
	    btnHireMercenary = new JButton("Hire Mercenary");
		btnHireMercenary.setBounds(35, 97, 147, 23);
		spaceStation.add(btnHireMercenary);
		
		btnShipYard = new JButton("Ship Yard");
		btnShipYard.setBounds(35, 131, 147, 23);
		spaceStation.add(btnShipYard);
		
		final JPanel playerInfo = new JPanel();
		playerInfo.setBorder(new SoftBevelBorder
				(BevelBorder.RAISED, null, null, null, null));
		playerInfo.setBounds(10, 186, 224, 120);
		getContentPane().add(playerInfo);
		playerInfo.setLayout(null);
		
		final JLabel lblPlayerInfo = new JLabel("Player Info");
		lblPlayerInfo.setBounds(4, 4, 90, 14);
		playerInfo.add(lblPlayerInfo);
		
		lblFuel = new JLabel("Fuel");
		lblFuel.setBounds(35, 29, 142, 14);
		playerInfo.add(lblFuel);
		
		lblHealth = new JLabel("Health");
		lblHealth.setBounds(35, 57, 142, 14);
		playerInfo.add(lblHealth);
		
		lblNewLabel = new JLabel("Level");
		lblNewLabel.setBounds(35, 84, 142, 14);
		playerInfo.add(lblNewLabel);
		setVisible(true);
	
	}
	
	/**
	 * Helper method to display the player's health on the screen
	 */
	public void setHealth(){
		lblHealth.setText("Health: " + model.getPlayer().getHealth());
	}
	
	/**
	 * Helper method that sets the name of the planet
	 * 
	 * @param j The index of the planet in the universe
	 */
	public void setPlanetName(int j){
		lblPlanetName.setText("Name: " + model.getUniverse().getPlanet(j).getName());
	}
	
	/**
	 * Helper method that sets the resource level of the planet
	 * 
	 * @param j The index of the planet in the universe
	 */
	public void setResource(int j){
		final Planet planet = model.getUniverse().getPlanet(j);
		final int x = planet.getResources();
		lblResource.setText("Resource: " + planet.getResourcesAt(x) );
	}
	
	/**
	 * Helper method that sets the tech level of the planet
	 * 
	 * @param j The index of the planet in the universe
	 */
	public void setTechLevel(int j){
		final Planet planet = model.getUniverse().getPlanet(j);
		final int x = planet.getTech();
		lblTechLevel.setText("Tech Level: " + planet.getTechLevel(x) );
	}

	/**
	 * Helper method to display the ship's fuel on the screen
	 */
	public void setFuel(){
		lblFuel.setText("Fuel: " + model.getShip().getFuel());
	}
	
	/**
	 * Helper method to display the player's currency on the screen
	 */
	public void setCurrency(){
		lblCurrency.setText("Currency: " + model.getPlayer().getCredits());
	}
	
	/**
	 * Method setBay.
	 */
	public void setBay(){
		lblBaysFilled.setText("Bay Filled: " + model.getShip().getCargo() + "/15");
	}
	
	/**
	 * Method setMarket.
	 * @param j int
	 */
	public void setMarket(int j) {
		for(int i=0; i < 10; i++){
			MarketPlace market =model.getUniverse().getPlanet(j).getMarket();
			Goods goods = market.getGoodsArray()[i];
			String text = goods.getCountString();
			buy[i].setText(text);
			text = model.getShip().getGoodsArray()[i].getSoldString();
			sell[i].setText(text);
			text = goods.getName();
			table_1.setValueAt(text, i, 0);
			int price = goods.getPrice();
			table_2.setValueAt(price, i, 0);
			price =goods.getPrice();
			table_5.setValueAt("" + (int) (price * .95), i, 0);
		}
	}
	
	/**
	 * Method resetSell.
	 */
	public void resetSell() {
		for(int i=0; i < 10; i++){
			sell[i].setText("0");
			table_4.setValueAt(0, i, 0);
		}
	}
	
	/**
	 * Method setSell.
	 */
	public void setSell() {
		for(int i=0; i < 10; i++){
			sell[i].setText("" + model.getShip().getGoodsArray()[i].getSold());
			table_4.setValueAt(0, i, 0);
		}
	}
	
	/**
	 * Method drawNav.
	 */
	public void drawNav(){
		panel = new NavPanel(model, parent);
		navigation.add(panel);
		panel.setBounds(10, 20, navigation.getWidth() - 20, navigation.getHeight() - 40);
		panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
	}
}
